package com.cy.pj.common.web;

import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import com.cy.pj.common.exception.ServiceException;

/**
 * SpringMVC中的拦截器
 * 控制层方法执行之前进行拦截
 * 
 */
public class TimeAccessInterceptor implements HandlerInterceptor {
	/**
	 * preHandle方法会在目标controller方法执行之前进行拦截
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//获取当前时间的日历对象
		Calendar c = Calendar.getInstance();
		//基于业务设定时间边界
		c.set(Calendar.HOUR_OF_DAY, 9);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		long start = c.getTimeInMillis();
		
		c.set(Calendar.HOUR_OF_DAY, 23);
		long end = c.getTimeInMillis();
		//基于当前时间与业务涉及时间进行放行和终止
		long cTime = System.currentTimeMillis();
		if (cTime<start||cTime>end)throw new  ServiceException("您不在访问时间范围之类");
		return true;//true表示放行(可以去执行controller里面的方法)
	}

}
